#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
  char buf[1024];
  ssize_t s=read(0,buf,sizeof(buf));    //这里用0是stdin键盘输入
  if(s<0)       //读取失败
  {
    perror("read");
    return 1;
  }

  buf[s]='\0';    //C语言中需要字符串结束标志，read返回的是数组下标,直接接着下标填入数组
  printf("echo:%s\n",buf);
  return 0;
}
//  const char *msg = "hello Linux\n";
//  write(3,msg,strlen(msg));
//  write(5,msg,strlen(msg));
  
//}

//int main()
//{
//  umask(0);
//  int fd1=open("log1.txt",O_WRONLY|O_CREAT|O_APPEND,0666);
//  int fd2=open("log2.txt",O_WRONLY|O_CREAT|O_APPEND,0666);
//  int fd3=open("log3.txt",O_WRONLY|O_CREAT|O_APPEND,0666);
//  int fd4=open("log4.txt",O_WRONLY|O_CREAT|O_APPEND,0666);
//  if(fd1<0)      //打开失败返回-1
//  {
//    printf("open error\n");
//    return 1;
//  }
//  printf("fd1:%d\n",fd1);
//  printf("fd2:%d\n",fd2);
//  printf("fd3:%d\n",fd3);
//  printf("fd4:%d\n",fd4);
//  const char *message = "abc";
//  write(fd,message,strlen(message));
//  close(fd);
//  return 0;
//}


/*
#define ONE (1<<0)     //1
#define TWO (1<<1)     //2
#define THREE (1<<2)   //4
#define FOUR (1<<3)    //8

//可以在内部传递不同的标志位展示不同的功能
void show(int flags)
{
  if(flags&ONE) printf("hello function1\n");
  if(flags&TWO) printf("hello function2\n");
  if(flags&THREE) printf("hello function3\n");
  if(flags&FOUR) printf("hello function4\n");
}

int main()
{
  printf("----------------\n");
  show(ONE);
  printf("----------------\n");
  show(TWO);
  printf("----------------\n");

  show(ONE|TWO);
  printf("----------------\n");
  show(ONE|TWO|THREE);
  printf("----------------\n");
  show(ONE|FOUR);
  printf("----------------\n");
}
*/

/*
int main()
{
  printf("pid:%d\n",getpid());
  FILE* fp = fopen("test.txt","a");         //打开一个文件
  if(fp==NULL)
  {
    perror("fopen");
    return 1;
  }
  const char* message = "hello Linux message\n";    //写入内容
  fwrite(message,strlen(message),1,fp);
  fclose(fp);                                     //退出
  return 0;
}
*/
/*
int main()
{
  //更改当前路径
  chdir("/home/ling");
  printf("pid:%d\n",getpid());
  //打开文件的路径和文件名，默认在当前路径下创建
  FILE* fp=fopen("testttttttttttttt.txt","w");
  if(fp==NULL)
  {
    perror("fopen");
    return 1;
  }
  fclose(fp);
  sleep(5000);
  return 0;
}
*/ 
